package com.abner.springcloudprovideruser.Service;

import com.abner.springcloudprovideruser.dao.UserMapper;
import com.abner.springcloudprovideruser.entity.User;
import com.abner.springcloudprovideruser.entity.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xijing.jiang
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User getUser(String id){
        return userMapper.selectByPrimaryKey(id);
    }

    public User getUserByIdAndUsername(String id,String username){
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andUsernameEqualTo(username);
        List<User> userList = userMapper.selectByExample(example);
        if (userList == null) {
            return null;
        }else{
            return userMapper.selectByExample(example).get(0);
        }
    }

    public int addUser(User user){
        return userMapper.insertSelective(user);
    }
}
